import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-08-09
 * Time: 22:04
 */
public class MySQLDelete {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java105?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("");

        Connection connection = dataSource.getConnection();
        System.out.println(connection);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除id:");
        int id = scanner.nextInt();

        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        int n = statement.executeUpdate();
        System.out.println("n="+n);
        System.out.println("statement:"+statement);

        statement.close();
        connection.close();

    }
}
